package swing.customComp;

import client.UserSession;
import swing.FrameManager;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GroupListMouseListener extends MouseAdapter {
    JList groupList;
    JPopupMenu groupMenu;
    JPopupMenu refreshMenu;


    public GroupListMouseListener(JList list, JPopupMenu menu){
        groupList = list;
        groupMenu = menu;

        refreshMenu = new JPopupMenu();
        JMenuItem refreshWhiteboard = new JMenuItem("刷新群组列表");
        refreshWhiteboard.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FrameManager.mainFrame.updateGroupList();
            }
        });
        refreshMenu.add(refreshWhiteboard);
    }

    @Override
    public void mouseDragged(MouseEvent e){
        if (groupList.locationToIndex(e.getPoint()) == -1)
            groupList.clearSelection();

    }

    public void mouseClicked(MouseEvent e) {
        // list.locationToIndex(e.getPoint()) 获取被点击的元素index
        // 左键 1   中 2   右键3
        if (e.getButton() == 3){
            groupMenu.show(groupList, e.getX(), e.getY());
            return;
        }

        int index = groupList.locationToIndex(e.getPoint());
        if (index == -1)
            return;

        if (e.getButton() == 1){
            groupList.setSelectedIndex(index);
            UserSession.setSelectedGroupIndex(index);

            FrameManager.mainFrame.updateMemberList();
            FrameManager.mainFrame.updateWhiteboardList();
        }


        // jList.setSelectedIndex(jList.locationToIndex(e.getPoint()));
    }
}
